package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author xgm
 * @date 2020/11/21
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //rpc
        Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Permission> findAllPermission() {
        return roleDao.findAllPermission();
    }

    @Override
    public List<Menu> findAllMenu() {
        return roleDao.findAllMenu();
    }

    @Transactional
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        log.info("[角色-添加]data:{},permissionIds：{},menuIds:{}",role,permissionIds, menuIds);
        // 插入基本数据
        roleDao.insert(role);
        //menuIds中只有子菜单
        //加入这些子菜单的父菜单
        for (Integer menuId : menuIds) {
            //先加入已有的子菜单
            roleDao.insertRoleAndMenu(role.getId(),menuId);
            Integer i = roleDao.findFuId(menuId);
            //判断是否已经存在这个父菜单在关系表中
            Long count = roleDao.findExist(role.getId(),i);
            if (count == 0){
                //关系表中加入父菜单
                roleDao.insertRoleAndMenu(role.getId(),i);
            }
        }

        //逐条插入关系表数据
        //角色有两个关系表
        //分别是
        //       角色和权限
        for (Integer permissionId : permissionIds) {
            roleDao.insertRoleAndPermission(role.getId(),permissionId);
        }
    }

    @Override
    public Role findRoleById(Integer id) {
        return roleDao.selectByRoleId(id);
    }

    @Override
    public List<Integer> findPermissionByRoleId(Integer id) {
        return roleDao.findAllPermissionByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findAllMenuByRoleId(id);
    }

    @Transactional
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //需要更新3张表
        //也就是先删旧数据，再插新数据
        //先更新角色
        roleDao.updata(role);
        //通过角色id对角色权限中间表进行删除
        roleDao.delectRole_permissionByRoleId(role.getId());
        roleDao.delectMenuByRoleId(role.getId());
        //加入新关系
        for (Integer permissionId : permissionIds) {
            roleDao.insertRoleAndPermission(role.getId(),permissionId);
        }
        for (Integer menuId : menuIds) {
            //这里只新增了子菜单
            roleDao.insertRoleAndMenu(role.getId(),menuId);
            log.info("[菜单Id]id:{},data:{}",menuId,role.getId());
            Integer i = roleDao.findFuId(menuId);
            if (null != i) {
                //判断是否已经存在这个父菜单在关系表中
                Long count = roleDao.findExist(role.getId(),i);
                if (count == 0){
                    //关系表中加入父菜单
                    roleDao.insertRoleAndMenu(role.getId(),i);
                }
            }

        }
    }

    @Transactional
    @Override
    public void delete(Integer id) {
        log.info("[删除角色]id:{}",id);
        //校验是否可以删除  如果有与这个角色绑定的用户则不能删除
        //先按照这个Id在t_user_role查找有没有user
        Long count = roleDao.countUserByRoleId(id);
        if (count > 0){
            throw new BusinessRuntimeException("当前角色有用户，不能删除");
        }
        //先删除与角色数据关联的中间表的数据
        roleDao.delectRole_permissionByRoleId(id);
        roleDao.delectMenuByRoleId(id);
        //删除角色数据
        roleDao.delete(id);
    }

}
